package com.pt.chitra.view.mainAppView
{
	import com.pt.chitra.config.ApplicationConfigs;
	import com.pt.chitra.config.ApplicationEventConstant;
	import com.pt.chitra.config.ApplicationNotificationConstant;
	import com.pt.chitra.view.albumView.view.AlbumViewMediator;
	import com.pt.chitra.view.editView.view.EditViewMediator;
	import com.pt.chitra.view.shareView.view.ShareViewMediator;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class MainViewMediator extends Mediator implements IMediator
	{
		/*name of the Mediator */
        public static const NAME:String = "ApplicationMainViewMediator";

		
		public function MainViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		//Do something -- Skulkarni
		/***
		 * Check for session object
		 * 
		 * */
		override public function onRegister():void
		{
			var a:Object = ApplicationConfigs.userSessionObject  
			if(ApplicationConfigs.userSessionObject !=null){
				mainView.appViewStack.selectedIndex = 1
			}else{
				mainView.appViewStack.selectedIndex = 0
			}
			/**
			 * Creation complete of 0th  element of stack never get called so register first child.
			 * */
			if(!facade.hasMediator(AlbumViewMediator.NAME)){
				facade.registerMediator(new AlbumViewMediator(mainView.albums));
			}
			
			mainView.addEventListener(ApplicationEventConstant.ALBUM_VIEW_STACK_COMPLETE,albumViewCreated);;
			mainView.addEventListener(ApplicationEventConstant.EDIT_VIEW_STACK_COMPLETE,albumViewCreated);;
			mainView.addEventListener(ApplicationEventConstant.SHARE_VIEW_STACK_COMPLETE,albumViewCreated);;
		}
		
		/**
		 * Swtich for Events-- Skulkarni
		 * */
		private function albumViewCreated(e:Event):void{
			switch(e.type){
				case ApplicationEventConstant.EDIT_VIEW_STACK_COMPLETE:
					if(!facade.hasMediator(EditViewMediator.NAME)){
						facade.registerMediator(new EditViewMediator(mainView.edit));
					}
				break;
				case ApplicationEventConstant.SHARE_VIEW_STACK_COMPLETE:
					if(!facade.hasMediator(ShareViewMediator.NAME)){
						facade.registerMediator(new ShareViewMediator(mainView.edit));
					}
				break;
			}
		}
		
		
		// Do Something -- Skulkarni
		override public function onRemove():void
		{
		}
			
		override public function listNotificationInterests():Array
		{
			return [
			ApplicationNotificationConstant.CHANGE_TO_EDIT_VIEW
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			 switch(notification.getName()){
				case ApplicationNotificationConstant.CHANGE_TO_EDIT_VIEW:
					mainView.mainTabBar.selectedIndex = 1;
				break;
			} 
		}
		
		protected function get mainView():MainAppView
        {
            return viewComponent as MainAppView;
        }

		
	}
}